package com.ibigcat.module.mdm.controller.admin.customersetgroup;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.ibigcat.framework.common.pojo.PageParam;
import com.ibigcat.framework.common.pojo.PageResult;
import com.ibigcat.framework.common.pojo.CommonResult;
import com.ibigcat.framework.common.util.object.BeanUtils;
import static com.ibigcat.framework.common.pojo.CommonResult.success;

import com.ibigcat.framework.excel.core.util.ExcelUtils;

import com.ibigcat.framework.apilog.core.annotation.ApiAccessLog;
import static com.ibigcat.framework.apilog.core.enums.OperateTypeEnum.*;

import com.ibigcat.module.mdm.controller.admin.customersetgroup.vo.*;
import com.ibigcat.module.mdm.dao.entity.CustomerSetGroupDO;
import com.ibigcat.module.mdm.service.CustomerSetGroupService;

@Tag(name = "管理后台 - 客户组明细")
@RestController
@RequestMapping("/mdm/customer-set-group")
@Validated
public class CustomerSetGroupController {

    @Resource
    private CustomerSetGroupService customerSetGroupService;

    @PostMapping("/create")
    @Operation(summary = "创建客户组明细")
    @PreAuthorize("@ss.hasPermission('mdm:customer-set-group:create')")
    public CommonResult<Long> createCustomerSetGroup(@Valid @RequestBody CustomerSetGroupSaveReqVO createReqVO) {
        return success(customerSetGroupService.createCustomerSetGroup(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新客户组明细")
    @PreAuthorize("@ss.hasPermission('mdm:customer-set-group:update')")
    public CommonResult<Boolean> updateCustomerSetGroup(@Valid @RequestBody CustomerSetGroupSaveReqVO updateReqVO) {
        customerSetGroupService.updateCustomerSetGroup(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除客户组明细")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mdm:customer-set-group:delete')")
    public CommonResult<Boolean> deleteCustomerSetGroup(@RequestParam("id") Long id) {
        customerSetGroupService.deleteCustomerSetGroup(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得客户组明细")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mdm:customer-set-group:query')")
    public CommonResult<CustomerSetGroupRespVO> getCustomerSetGroup(@RequestParam("id") Long id) {
        CustomerSetGroupDO customerSetGroup = customerSetGroupService.getCustomerSetGroup(id);
        return success(BeanUtils.toBean(customerSetGroup, CustomerSetGroupRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得客户组明细分页")
    @PreAuthorize("@ss.hasPermission('mdm:customer-set-group:query')")
    public CommonResult<PageResult<CustomerSetGroupRespVO>> getCustomerSetGroupPage(@Valid CustomerSetGroupPageReqVO pageReqVO) {
        PageResult<CustomerSetGroupDO> pageResult = customerSetGroupService.getCustomerSetGroupPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CustomerSetGroupRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出客户组明细 Excel")
    @PreAuthorize("@ss.hasPermission('mdm:customer-set-group:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCustomerSetGroupExcel(@Valid CustomerSetGroupPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CustomerSetGroupDO> list = customerSetGroupService.getCustomerSetGroupPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "客户组明细.xls", "数据", CustomerSetGroupRespVO.class,
                        BeanUtils.toBean(list, CustomerSetGroupRespVO.class));
    }

}